﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Grassroots;
using ZHDJ.WebPage.Master;
using ZHDJ.WebPage;
using ZHDJ.Data.Models.Common;
using FineUI;

namespace ZHDJ.Web.Portal.Grassroots
{
    public partial class MassesVoiceMore : UseMorePageMasterPage
    {
        /// <summary>
        /// ItemID
        /// </summary>
        private string ItemID
        {
            get
            {
                return this.Request["ItemID"] ?? "null";
            }
        }

        /// <summary>
        /// QueryStatus
        /// </summary>
        private string QueryStatus
        {
            get
            {
                return this.Request["QueryStatus"] ?? "";
            }
        }

        /// <summary>
        /// Search
        /// </summary>
        private string QueryText
        {
            get
            {
                return this.Request["QueryText"] ?? "";
            }
        }
        /// <summary>
        /// 页数
        /// </summary>
        private string PageIndex
        {
            get
            {
                return this.Request["PageIndex"] ?? "";
            }
        }

        /// <summary>
        /// 每页条数
        /// </summary>
        private string PageSize
        {
            get
            {
                return this.Request["PageSize"] ?? "";
            }
        }
        private string DataCount
        {
            get
            {
                return this.Request["DataCount"] ?? "";
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!string.IsNullOrEmpty(this.PageIndex))
            {
                this.WriteList();
                return;
            }
            else if (!string.IsNullOrEmpty(this.DataCount))
            {
                WriteDataCount();
                return;
            }
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }

        }
        private void BindData()
        {
            this.txtG.Text = PartyGrassrootsGroupItem.QueryAllNameByItemID(ItemID);
            txtUserName.Text = CurrentUser == null ? "" : CurrentUser.UserName;
            txtMobileNo.Text = CurrentUser == null ? "" : CurrentUser.MobileNo;
        }
        public void LoadData()
        {
            hdItemID.Value = ItemID;
        }

        private void WriteList()
        {
            DataTable dt = PartyGrassrootsSuggestion.GetGridData(Convert.ToInt32(this.PageIndex), Convert.ToInt32(PageSize),
                QueryText, QueryStatus, "", "", "", "", ItemID, "1").Tables[0];

            dt.Columns.Add("CollectionTime2", typeof(string));
            dt.Columns.Add("ModifyTime2", typeof(string));
            foreach (DataRow dr in dt.Rows)
            {
                dr["CollectionTime2"] = Convert.ToDateTime(dr["CollectionTime"]).ToString("yyyy-MM-dd HH:mm");
                dr["ModifyTime2"] = (dr["ModifyTime"].ToString() == "" || dr["StatusName"].ToString() == "待回复") ? "" : Convert.ToDateTime(dr["ModifyTime"]).ToString("yyyy-MM-dd HH:mm");
                dr["MobileNo"] = StringHelper.MobileNoHide(Convert.ToString(dr["MobileNo"]));
            }
            var json = JsonConvert.SerializeObject(dt);
            Response.Clear();
            Response.Write(json);
            Response.End();
        }
        private void WriteDataCount()
        {
            Response.Clear();
            Response.Write(PartyGrassrootsSuggestion.GetGridData(0, 10, QueryText, QueryStatus, "", "", "", "", ItemID, "1").Tables[1].Rows[0][0]);
            Response.End();
        }

        #region 提交

        public bool CheckValue()
        {

            var rs = true;

            if (this.txtTitle.Text.Trim() == "")
            {
                this.txtTitle.MarkInvalid("必填");
                rs = false;
            }
            if (this.txtUserName.Text.Trim() == "")
            {
                this.txtUserName.MarkInvalid("必填");
                rs = false;
            }
            if (this.txtMobileNo.Text.Trim() == "")
            {
                this.txtMobileNo.MarkInvalid("必填");
                rs = false;
            }
            if (!KeyWord.IsContainSensitiveWords(this.txtTitle.Text))
            {
                this.txtTitle.MarkInvalid("您输入的主题存在敏感词");
                rs = false;
            }

            return rs;
        }


        public void SaveData()
        {
            if (string.IsNullOrEmpty(ItemID))
            {
                PageContext.RegisterStartupScript(FineUI.Alert.GetShowReference("参数错误。", "提示", "su();"));
                return;
            }
            if (!CheckValue())
                return;
            var saveModel = new PartyGrassrootsSuggestion();
            saveModel.ID = StringHelper.NewID;
            saveModel.ItemID = ItemID;
            saveModel.SerialNumber = "";
            saveModel.Title = txtTitle.Text;
            saveModel.MobileNo = txtMobileNo.Text;
            saveModel.UserName = txtUserName.Text;
            saveModel.CollectionTime = DateTime.Now;
            saveModel.Content = this.Request.Form["editorValue"];
            saveModel.Status = 1;
            saveModel.CreateTime = DateTime.Now;
            saveModel.CreateBy = CurrentUser == null ? "" : CurrentUser.LoginID;
            saveModel.ModifyTime = DateTime.Now;
            saveModel.ModifyBy = CurrentUser == null ? "" : CurrentUser.LoginID; ;
            saveModel.publicity = cbxPublicity.Checked ? 1 : 0;
            saveModel.Reply = "";
            DB.PartyGrassrootsSuggestions.Add(saveModel);
            if (DB.SaveChanges() > 0)
            {
                PageContext.RegisterStartupScript(FineUI.Alert.GetShowReference("信息已提交。", "提示", "su();"));
            }
            else
            {
                FineUI.Alert.Show("提交失败，请稍后再试。");
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        #endregion
        #region 模块菜单
        /// <summary>
        /// 初始化module  如果通过url传参可不设置
        /// </summary>
        protected override void InitModule()
        {
            Category = "GrassrootsSuggestion";
            //Category = "";

        }
        #endregion
    }
}